//
//  LALoginViewController.m
//  lingAo
//
//  Created by saxon on 16/9/20.
//  Copyright © 2016年 saxon. All rights reserved.
//

#import "LALoginViewController.h"
#import "LAWebViewController.h"
#import "KeyChainHelper.h"
@interface LALoginViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *bgImageView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITextField *usernameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UILabel *errorLabel;
@property (weak, nonatomic) IBOutlet UIView *subView;
@property (weak, nonatomic) IBOutlet UIButton *subBtn;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activity;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *logoViewHeight;
@property (weak, nonatomic) IBOutlet UIImageView *logoView;

@end

@implementation LALoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([KeyChainHelper serviceForKey:@"username"] || [KeyChainHelper serviceForKey:@"password"]) {
        self.usernameTextField.text = [KeyChainHelper serviceForKey:@"username"];
        self.passwordTextField.text = [KeyChainHelper serviceForKey:@"password"];
    }
    
   
    
    self.usernameTextField.textColor = HTMLCOLOR(@"#333333");
    self.passwordTextField.textColor = self.usernameTextField.textColor;
    
    if (IsIPhone6P) {
        self.bgImageView.image = IMAGE(@"1080 (1).png");
    }
    
    self.titleLabel.textColor = HTMLCOLOR(@"#2d8bff");
    self.subView.backgroundColor = HTMLCOLOR(@"#066deb");
    self.errorLabel.textColor = HTMLCOLOR(@"#ff3e3e");
    
    [self.subBtn setBackgroundImage:[RTToolObject imageFromColor:HTMLCOLOR(@"#066deb") frame:CGRectMake(0, 0, SCREEN_W, 44)] forState:UIControlStateNormal];
    [self.subBtn setBackgroundImage:[RTToolObject imageFromColor:HTMLCOLOR(@"#2d8bff") frame:CGRectMake(0, 0, SCREEN_W, 44)] forState:UIControlStateHighlighted];
    
    [self.activity startAnimating];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardShowHide:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardShowHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)processBtn:(UIButton *)sender {
    NSString *username = [NSString stringWithFormat:@"%@", self.usernameTextField.text];
    NSString *password = [NSString stringWithFormat:@"%@", self.passwordTextField.text];
    if (!username || [username isEqualToString:@""]) {
        self.errorLabel.hidden = NO;
        self.errorLabel.text = @"请输入用户名";
        return;
    } else if (!password || [password isEqualToString:@""]) {
        self.errorLabel.hidden = NO;
        self.errorLabel.text = @"请输入密码";
        return;
    }
    self.errorLabel.hidden = YES;
    sender.hidden = YES;
    [self.activity startAnimating];
    
    
    NSMutableDictionary *postDic = [NSMutableDictionary dictionaryWithCapacity:0];
    [postDic setObject:username forKey:@"username"];
    [postDic setObject:password forKey:@"password"];
    
    [RTHttpClient Post:[NSString stringWithFormat:@"http://%@/mobileweb/login/checkLogin", HOST] data:postDic success:^(NSDictionary *data) {
        
        [KeyChainHelper addService: username withKey:@"username"];
        [KeyChainHelper  addService:password withKey:@"password"];
        
        PublicModel *model = [PublicModel objectWithKeyValues:data];
        
        
        
        if (model.cn == 0) {
            NSString *cookie = [model.data objectForKey:@"mobilehousehold"];
            NSString *token = [USER_DEFAULT_t objectForKey:@"deviceToken"];
            token = [token hasValue] ? token : @"";
            [self setCookie:cookie];
            [RTHttpClient Post:[NSString stringWithFormat:@"http://%@/mobileweb/index/addEditToken", HOST] data:@{@"type":@"ios", @"token":token, @"cookie":cookie} success:^(NSDictionary *data) {
            } fail:^(NSString *error) {
            }];
        } else {
            self.errorLabel.hidden = NO;
            self.errorLabel.text = model.message;
        }
        [self.activity stopAnimating];
        sender.hidden = NO;
    } fail:^(NSString *error) {
        
        [self.activity stopAnimating];
        sender.hidden = NO;
    }];
}
- (void)setCookie:(NSString *)cookie
{
    if (![cookie hasValue]) {
        return;
    }
    [USER_DEFAULT_t setObject:cookie forKey:@"cookie"];
    
    LAWebViewController *webVc = [[LAWebViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:webVc];
    [self presentViewController:nav animated:YES completion:nil];
    [UIApplication sharedApplication].keyWindow.rootViewController = nav;
    
}
- (void)keyBoardShowHide:(NSNotification *)sender
{
    if (IsiPhone4 || IsIPhone5) {
        __block CGRect frame = self.logoView.frame;
        [UIView animateWithDuration:1 animations:^{
            if ([sender.name isEqualToString:UIKeyboardWillShowNotification]) {
                self.logoViewHeight.constant = 0;
                frame.size.height = 0;
                self.logoView.frame = frame;
            } else {
                self.logoViewHeight.constant = 100;
                frame.size.height = 100;
                self.logoView.frame = frame;
            }
        }];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
